
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:love_app/common/su_dialog.dart';
import 'package:love_app/config/app.dart';
import 'package:love_app/config/providers_init.dart';
import 'package:love_app/init/notifications_init.dart';
import 'package:love_app/pages/ad_page/ad_page.dart';
import 'package:love_app/routes/application.dart';
import 'package:love_app/routes/routes.dart';
import 'package:provider/provider.dart';
import 'dart:io';


FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin; // 状态栏推送

// 入口文件
void main() {

  WidgetsFlutterBinding.ensureInitialized(); // 可以再runApp()函数之前使用一些特殊的方
  // 强制竖屏
  SystemChrome.setPreferredOrientations([
    DeviceOrientation.portraitUp,
    DeviceOrientation.portraitDown,
  ]);
  // 路由注册
  Router router = Router();
  Routes.configureRouters(router);
  Application.router = router;
  Provider.debugCheckInvalidValueType = null; // 取消Provider异常提示
  runApp(
    MultiProvider(
      // 聚合方式添加多个共享数据
      providers: InitProviders.allProvider(),
      child: MyApp(),
    ),
  );

  // /// 自定义报错页面
  // ErrorWidget.builder = (FlutterErrorDetails flutterErrorDetails) {
  //   debugPrint(flutterErrorDetails.toString());
  //   return SuDialog.showToast("app异常!!!");
  // };

  /// Android状态栏透明
  if (Platform.isAndroid) {
    SystemUiOverlayStyle systemUiOverlayStyle = SystemUiOverlayStyle(
      statusBarColor: Colors.transparent,
      statusBarBrightness: Brightness.light,
    );
    SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
  }
}

// 入口
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
    NotificationInit(context).init(flutterLocalNotificationsPlugin);
    return MaterialApp(
      title: App.appTitle,
      localizationsDelegates: [
        // 本地化的代理类
        GlobalEasyRefreshLocalizations.delegate, // 上拉加载主件汉化
        GlobalMaterialLocalizations.delegate,
        // GlobalWidgetsLocalizations.delegate,
      ],
      supportedLocales: [
        const Locale('en', 'US'), // 美国英语
        const Locale('zh', 'CN'), // 中文简体
        //其它Locales
      ],
      showPerformanceOverlay: App.showPO, // 显示GPU和曲线图，用于性能测试
      debugShowCheckedModeBanner: App.showDebug,
      onGenerateRoute: Application.router.generator,
      navigatorKey: Application.navigatorKey,
      theme: _myThemeData(),
      home: AdPage(), // 广告页面
    );
  }

  // 全局主题定义
  ThemeData _myThemeData() {
    return ThemeData(
      scaffoldBackgroundColor: App.scaffoldBackgroundColor,
    );
  }
}
